package com.atguigu.Flink.sql.tableAPI;

import com.atguigu.Flink.POJO.Event;
import com.atguigu.Flink.function.ClickSource;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Expressions;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import static org.apache.flink.table.api.Expressions.*;


public class Flink01_HelloWorld {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //基于流式执行环境创建表式执行环境
        StreamTableEnvironment tableEnvironment = StreamTableEnvironment.create(env);

        DataStreamSource<Event> ds = env.addSource(new ClickSource());
        //将流转换为表
        Table table = tableEnvironment.fromDataStream(ds);
        //打印表结果
        table.printSchema();
        //查询表中数据
        Table result = table.where($("user").isEqual("otto"))
                .select($("user"), $("url"), $("ts"));
        //打印表中数据
        result.execute().print();

        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
